package com.example.boot.modules.system.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.boot.modules.system.constant.SMS;
import com.example.boot.modules.system.convert.UserConvert;
import com.example.boot.modules.system.dto.RegisterParam;
import com.example.boot.modules.system.dto.ResetPasswordDto;
import com.example.boot.modules.system.dto.ResetSelfDto;
import com.example.boot.modules.system.dto.UserInfo;
import com.example.boot.modules.system.entity.User;
import com.example.boot.modules.system.service.IUserService;
import com.example.starter.security.anntation.AuthUser;
import com.example.starter.security.pojo.ExampleUser;
import com.example.starter.security.util.PasswordUtil;
import com.example.starter.util.Response;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @author 王令
 * @since 2022/12/17
 */
@RestController
@RequestMapping("/system/self")
@RequiredArgsConstructor
public class SelfController {

    private final IUserService service;
    private final RedisTemplate<String, Object> redisTemplate;

    @PostMapping("/register")
    public Response<Boolean> register(@RequestBody @Validated RegisterParam param) {
        // 校验验证码
        final String code = (String) redisTemplate.opsForValue().get(SMS.REGISTER_CODE + param.getPhone());
        if (!StrUtil.equals(code, param.getCode())) {
            return Response.fail("验证码不正确");
        }
        // 校验手机号是否已注册
        final User one = service.getOne(Wrappers.<User>lambdaQuery().select(User::getId).eq(User::getPhone, param.getPhone()));
        if (ObjectUtil.isNotNull(one)) {
            return Response.fail("手机号已注册");
        }
        redisTemplate.delete(SMS.REGISTER_CODE + param.getPhone());
        User user = new User();
        user.setPhone(param.getPhone());
        user.setPassword(PasswordUtil.encode(param.getPassword()));
        user.setUsername(service.nextUid());
        user.setEnabled(true);
        user.setNonLocked(true);
        service.save(user);
        return Response.ok();
    }

    @PutMapping("/reset")
    public Response<Boolean> reset(@RequestBody ResetSelfDto param, @AuthUser ExampleUser user) {
        service.update(UserConvert.INSTANCE.toUser(param), Wrappers.<User>lambdaUpdate()
                .eq(User::getUsername, user.getUsername()));
        return Response.ok();
    }

    @PutMapping("/reset_password")
    public Response<Boolean> resetPassword(@RequestBody @Validated ResetPasswordDto param) {
        // 校验验证码
        String code = (String) redisTemplate.opsForValue().get(SMS.RESET_PASSWORD_CODE + param.getPhone());
        if (!StrUtil.equals(code, param.getCode())) {
            return Response.fail("验证码不正确");
        }
        // 校验手机号是否注册
        User user = service.getOne(Wrappers.<User>lambdaQuery().select(User::getId).eq(User::getPhone, param.getPhone()));
        if (ObjectUtil.isNull(user)) {
            return Response.fail("手机号尚未注册");
        }
        user.setPassword(PasswordUtil.encode(param.getPassword()));
        service.updateById(user);
        return Response.ok();
    }


    @GetMapping("/info")
    public Response<UserInfo> findInfo(@AuthUser ExampleUser user) {
        UserInfo info = service.findUserInfoByUsername(user.getUsername());
        return Response.ok(info);
    }

}
